package com.news.web.servlet.admin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.service.AdminService;
import com.news.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AdminAdminDeleteAllServlet
 */
@WebServlet("/AdminAdminDeleteAllServlet")
public class AdminAdminDeleteAllServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        // 创建adminService实例
        AdminService adminService = new AdminServiceImpl();

        // 获取传入数据
        String[] adminIdStr = request.getParameterValues("adminIdAll");

        // 检查adminIdStr是否为null或者长度为0
        if (adminIdStr != null && adminIdStr.length > 0) {
            int[] adminIds = new int[adminIdStr.length];
            for (int i = 0; i < adminIdStr.length; i++) {
                try {
                    adminIds[i] = Integer.parseInt(adminIdStr[i]);
                } catch (NumberFormatException e) {
                    // 如果失败，设置为null
                    request.setAttribute("adminDeleteAllBoolean", null);
                    // 跳转回原页面
                    request.getRequestDispatcher("/admin/adminAdmin.jsp").forward(request, response);
                    return;
                }
            }

            // 执行删除方法
            boolean adminDeleteAllBoolean = adminService.deleteAdmin(adminIds);

            // 将数据存入request属性中
            request.setAttribute("adminDeleteAllBoolean", adminDeleteAllBoolean);
        } else {
            // 如果adminIdStr为空，设置属性并跳转
            request.setAttribute("adminDeleteAllBoolean", false);
        }

        // 跳转回原页面
        request.getRequestDispatcher("/admin/adminAdmin.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
